from flask import render_template,redirect,request
from . import api_6
from .common import *
from .parse import *
from ..models import News
from .result import result

# 解析验证码
@api_6.route('/captcha',methods=['GET','POST'])
def captcha():
    if request.method=='POST':
        try:
            captcha_result = parse_captcha(request.files['captcha'])
            return result(110) if captcha_result==None else result(100,[{'result':captcha_result}])
        except Exception as e:
            return result(105)
    else:
        return result(102)


# 接收用户信息，保存到数据库
@api_6.route('/upload/userinfo',methods=['GET','POST'])
def userinfo():
    if request.method=='POST':
        try:
            return save_user_info(request.form['uid'],request.form['data'])
        except Exception as e:
            return result(103)
    else:
        return result(102)

# 接收课程表数据，保存到数据库
@api_6.route('/upload/courses',methods=['GET','POST'])
def courses():
    if request.method=='POST':
        try:
            return save_courses_text(request.form['uid'],request.form['data'])
        except Exception as e:
            return result(103)
    else:
        return result(102)

# 接收原始课程表 HTML 文件，解析并保存
@api_6.route('/parse/courses',methods=['GET','POST'])
def parse_courses():
    if request.method=='POST':
        try:
            return save_courses_html(request.form['uid'],request.files['html'])
        except Exception as e:
            return result(103)
    else:
        return result(102)


# 接收成绩表数据，保存到数据库
@api_6.route('/upload/scores',methods=['GET','POST'])
def upload_scores():
    if request.method=='POST':
        try:
            return save_scores_text(request.form['uid'],request.form['data'])
        except Exception as e:
            return result(103)
    else:
        return result(102)

# 接收原始成绩表 HTML 文件，解析并保存
@api_6.route('/parse/scores',methods=['GET','POST'])
def parse_scores():
    if request.method=='POST':
        try:
            return save_scores_html(request.form['uid'],request.files['html'])
        except Exception as e:
            return result(103)
    else:
        return result(102)


# 接收考试表数据，保存到数据库
@api_6.route('/upload/exams',methods=['GET','POST'])
def upload_exams():
    if request.method=='POST':
        try:
            return save_exams_text(request.form['uid'],request.form['data'])
        except Exception as e:
            return result(103)
    else:
        return result(102)

# 接收原始考试表 HTML 文件，解析并保存
@api_6.route('/parse/exams',methods=['GET','POST'])
def parse_exams():
    if request.method=='POST':
        try:
            return save_exams_html(request.form['uid'],request.files['html'])
        except Exception as e:
            return result(103)
    else:
        return result(102)

# 获取新闻列表
@api_6.route('/newslist',methods=['GET','POST'])
def newslist():
    if request.method=='GET':
        try:
            page = request.args.get('page',1,type=int)
            size = request.args.get('size',10)
        except Exception as e:
            return result(103)

        # 按照发布时间降序排序
        pagination = News.query.order_by(-News.pubTime).paginate(page,per_page=size,error_out=False)

        newslist = pagination.items
        if len(newslist) == 0:
            return result(109)
        return result(100,[news.to_json_list() for news in newslist])
    else:
        return result(102)

# 获取指定新闻详情
@api_6.route('/newsdetail',methods=['GET','POST'])
def newsdetail():
    if request.method=='GET':
        try:
            nid = request.args.get('nid',0,type=int)
        except Exception as e:
            return result(103)

        news = News.query.filter_by(nid=nid).first()
        if news == None:
            return result(109)
        return result(100,[news.to_json_detail()])
    else:
        return result(102)

# 检查更新
@api_6.route('/checkupdate',methods=['GET','POST'])
def check_update():
    if request.method=='POST':
        try:
            return check_client_update(int(request.form['version']))
        except KeyError as e:
            return result(103)
    else:
        return result(102)

# 请求启动页图片链接
@api_6.route('/launchimage',methods=['GET','POST'])
def launch_img():
    if request.method=='GET':
        return request_launch_img()
    else:
        return result(102)
